package baratopradedeu

import java.util.*
import java.text.*
import groovy.lang.Closure.*

class PedidoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [pedidoInstanceList: Pedido.list(params), pedidoInstanceTotal: Pedido.count()]
    }

    def create = {
        def pedidoInstance = new Pedido()
        pedidoInstance.properties = params
        return [pedidoInstance: pedidoInstance]
    }

    def save = {
        def pedidoInstance = new Pedido(params)
        if (pedidoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'pedido.label', default: 'Pedido'), pedidoInstance.id])}"
            return [pedidoInstance: pedidoInstance]
            redirect(action: "gerarCupom", id: pedidoInstance.id)
        }
        else {
            render(view: "list", model: [pedidoInstance: pedidoInstance])
        }
    }

    def show = {
        def pedidoInstance = Pedido.get(params.id)
        if (!pedidoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
        else {
            [pedidoInstance: pedidoInstance]
        }
    }

    def edit = {
        def pedidoInstance = Pedido.get(params.id)
        if (!pedidoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [pedidoInstance: pedidoInstance]
        }
    }

    def update = {
        def pedidoInstance = Pedido.get(params.id)
        if (pedidoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pedidoInstance.version > version) {
                    
                    pedidoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'pedido.label', default: 'Pedido')] as Object[], "Another user has updated this Pedido while you were editing")
                    render(view: "edit", model: [pedidoInstance: pedidoInstance])
                    return
                }
            }
            pedidoInstance.properties = params
            if (!pedidoInstance.hasErrors() && pedidoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'pedido.label', default: 'Pedido'), pedidoInstance.id])}"
                redirect(action: "show", id: pedidoInstance.id)
            }
            else {
                render(view: "edit", model: [pedidoInstance: pedidoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def pedidoInstance = Pedido.get(params.id)
        if (pedidoInstance) {
            try {
                pedidoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
    }
    
    def comprar = {
		if(session.cliente){
			def ofertaInstance = Oferta.findById(params.ofertaId)
			//def ofertaInstance = Oferta.get(params.id)
			//def pedidoInstance = ofertaInstance
			//pedidoInstance = Pedido.get(params.id)
			if (!ofertaInstance) {
				flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
				redirect(controller: "oferta", action: "list")
			}
			[ofertaId:params.ofertaId, ofertaInstance:ofertaInstance]
		}else{
			redirect(controller:'cliente',action:'login')
		}
    }

    def salvarCompra = {
		def cliente = Cliente.get(params.clienteId)
        def pedidoInstance = new Pedido(params)
        def of = Oferta.get(params.ofertaId)		
        pedidoInstance.cliente = cliente		     
        if (pedidoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [of.titulo, pedidoInstance.id])}"
            redirect(action: "gerarCupom", controller:"cupom", id: pedidoInstance.id)
        }
        else {
            render(controller:"pedido", view: "comprar", model: [pedidoInstance: pedidoInstance, ofertaId:params.ofertaId])
        }
    }

    def gerarCupom = {
		def cliente = Cliente.get(params.clienteId)
        def pedidoInstance = new Pedido(params)
        def of = Oferta.get(params.ofertaId)
		pedidoInstance.cliente = cliente       

        if (pedidoInstance) {
            def cupomInstance = new Cupom(params)
            cupomInstance.dataCompra = new Date()
			cupomInstance.dataValidade = of.dataFinal()
			System.out.println(cupomInstance.dataValidade)
            cupomInstance.pedido = pedidoInstance
			cupomInstance.oferta = of	
			cupomInstance.statusCupom = true
			System.out.println("teste")  
			
            return [cupomInstance: cupomInstance]
            //render(controller:"cupom", view: "salvarCupom", model: [pedidoInstance: pedidoInstance, cupomInstance:cupomInstance])
            //redirect(controller: "cupom", action: "salvarCupom", id: pedidoInstance.id)
        }
        else {
            render(controller:"cupom", view: "create", model: [pedidoInstance: pedidoInstance, ofertaId:params.ofertaId])
        }
    }

}